import {
    getQrProjectList
} from '../../api/long-insure'
import { fastInsureSubtractType } from '../../utils/map.js'
import { shake } from '../../../utils/util.js'
Page({
    /**
     * 页面的初始数据
     */
    data: {
        statusBarHeight: 0,
        showDialog: false,
        isScrollBottom: false,
        list: [],
        pageSize: 3,
        pageNum: 1,
        projectName: '',
        container: null,
        hasNextPage: true,
        opacityScale: 0
    },

    /**
     * 生命周期函数--监听页面加载
     */
    async onLoad() {
        this.setData({
            pageNum: 1,
            showDialog: !wx.getStorageSync('clickedFastInsureAgreement')
        })
        this.getList()
    },
    onShow() {
    },
    onPageScroll(e) {
        let scrollTop = e.scrollTop
        if(e.scrollTop > 90) {
            scrollTop = 90
        }
        const opacityScale = scrollTop / 90
        this.setData({
            opacityScale
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {
        // this.setData({
        //     container: () => wx.createSelectorQuery().select('.content-container')
        // })
        wx.createSelectorQuery().select('.custom-title').boundingClientRect(res => {
            console.log(1111, res.height)
            this.setData({
                statusBarHeight: res.height
            })
        }).exec()
    },
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {
        this.setData({
            pageNum: 1
        })
        this.getList()
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
        if (this.data.hasNextPage) {
            this.data.pageNum++
            this.getList()
        }
    },
    onScrollBottom() {
        this.setData({
            isScrollBottom: true,
        })
    },
    onConfirmAgreement() {
        wx.setStorageSync('clickedFastInsureAgreement', true)
        this.setData({
            showDialog: false			
        })
    },
    toAdd() {
        wx.navigateTo({
            url: '/packageLongInsure/pages/fast-insure-add/fast-insure-add',
        })
    },
    async getList() {
        const res = await getQrProjectList({
            pageSize: this.data.pageSize,
            pageNum: this.data.pageNum,
            projectName: this.data.projectName
        })
        res.list.forEach(i => {
            i.typeName = fastInsureSubtractType[i.type]
        })
        let listData = []
        if (this.data.pageNum === 1) {
            listData = res.list
        } else {
            listData = [...this.data.list, ...res.list]
        }
        this.setData({
            list: listData,
            hasNextPage: res.hasNextPage
        })
        wx.stopPullDownRefresh()
    },
    onInput(ev) {
        const dataset = ev.currentTarget.dataset
        this.setData({
            [dataset.prop]: ev.detail.value.trim(),
        })
        if (dataset.prop === 'projectName') {
            shake(() => {
                this.data.pageNum = 1
                this.getList()
            })
        }
    },
    onToInsureDetail(ev) {
        const dataset = ev.currentTarget.dataset
        wx.navigateTo({
            url: `/packageLongInsure/pages/fast-insure-detail/fast-insure-detail?id=${dataset.id}`,
        })
    }
})